package com.leicx.guli.common.config;

import com.alibaba.csp.sentinel.adapter.servlet.callback.WebCallbackManager;
import com.leicx.guli.common.enums.GuliExceptionEnum;
import com.leicx.guli.common.utils.JsonUtil;
import com.leicx.guli.common.utils.R;
import org.springframework.boot.SpringBootConfiguration;

/**
 * 自定义sentinel返回信息
 *
 * @author daxiong
 * @date 2022/8/22 下午4:50
 */
@SpringBootConfiguration
public class MySentinelConfig {

    public MySentinelConfig() {
        WebCallbackManager.setUrlBlockHandler((request, response, ex) -> {
            R error = R.error(GuliExceptionEnum.TOO_MANY_REQUEST);
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json");
            response.getWriter().write(JsonUtil.toJsonString(error));
        });
    }
}
